﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace TrivialPurfuit.Data
{
    class QuestionData
    {
        private const string DATA_FILE_NAME = "TPQuestions.xml";

         /// <summary>
        /// The data set used by the application.
        /// </summary>
        private TPDataSet tpDataSet;

        /// <summary>
        /// This is a singleton class, this is the static instance of this class.
        /// </summary>
        private static QuestionData _Instance;

        /// <summary>
        /// Gets the singleton instance of the MyData class.
        /// </summary>
        /// <returns>Singelton instance of the MyData class</returns>
        public static QuestionData Instance
        {
            get
            {
                if (QuestionData._Instance == null)
                    _Instance = new QuestionData();

                return QuestionData._Instance;
            }
        }

        /// <summary>
        /// Gets the next ID for the given table name.
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public int getNextID(string tableName)
        {
            int id = 0;

            DataTable tableToGetIDFrom = this.tpDataSet.Tables[tableName];
            
            DataRowCollection rows = this.tpDataSet.Tables[tableName].Rows;

            foreach (DataRow row in rows)
            {
                int temp = Convert.ToInt32(row["id"]);

                if (temp > id)
                    id = temp;
            }

            return id + 1;
        }

        /// <summary>
        /// Local referance of the Account table.
        /// </summary>
        public TPDataSet.QuestionDataTable Question;

        /// <summary>
        /// Local referance of the Account Type table.
        /// </summary>
        public TPDataSet.CategoryDataTable Catagory;


        /// <summary>
        /// Prevents instantiation of this class. Instantiates the singleton instance of this
        /// class.
        /// </summary>
        private QuestionData()
        {
            // Initialize the dataset
            this.tpDataSet = new TPDataSet();

            readData();

            // Bring table referances up a level so they are available localy.
            this.Question = this.tpDataSet.Question;
            this.Catagory = this.tpDataSet.Category;
        }

        public void readData()
        {
            if (File.Exists(DATA_FILE_NAME))
            {
                // Read the data from the xml file
                try
                {
                    this.tpDataSet.ReadXml(DATA_FILE_NAME);
                }
                catch (System.Security.SecurityException e)
                {
                    string temp = e.ToString();
                    System.Windows.MessageBox.Show(temp, "Error", System.Windows.MessageBoxButton.OK);
                }

            }
            else
            {
                this.addRequiredCatagoryRows();
            }
        }

        public void saveData()
        {
            try
            {
                this.tpDataSet.WriteXml(DATA_FILE_NAME);
            }
            catch (System.Security.SecurityException e)
            {
                string temp = e.ToString();
                System.Windows.MessageBox.Show(temp, "Error", System.Windows.MessageBoxButton.OK);
            }
        }

        private void addRequiredCatagoryRows()
        {
            ////////////////////////////
            // Required Null Catagory
            if (this.tpDataSet.Category.Rows.Count == 0)
            {
                this.tpDataSet.Category.AddCategoryRow(CategoryCON.NULL.ID, CategoryCON.NULL.Name, "Gray");
                this.tpDataSet.Category.AddCategoryRow(1, "People", "Red");
                this.tpDataSet.Category.AddCategoryRow(2, "Events", "White");
                this.tpDataSet.Category.AddCategoryRow(3, "Places", "Blue");
                this.tpDataSet.Category.AddCategoryRow(4, "Independence Day", "Green");
            }
        }


    }
}
